package org.jivesoftware.spark.crowd.packet;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.jivesoftware.smack.provider.ProviderManager;
import org.jivesoftware.spark.crowd.Param;
import org.xmlpull.v1.XmlPullParser;

public class IandAParser implements PacketExtensionProvider {
	private static IandAParser instance;

	public static final String ELEMENT_NAME = Param.CROWD_SERVICE_NAME;

	public static IandAParser getInstance() {
		if (instance == null)
			instance = new IandAParser();
		return instance;
	}

	public IandAParser() {
		ProviderManager.getInstance().addExtensionProvider(
				Param.CROWD_SERVICE_NAME, Param.apply_NS, this);
		ProviderManager.getInstance().addExtensionProvider(
				Param.CROWD_SERVICE_NAME, Param.invite_NS, this);
	}

	@Override
	public PacketExtension parseExtension(XmlPullParser parser)
			throws Exception {
		System.out.println("parse extension...");
		Extension ex = new Extension();

		ex.setCrowdName(parser.getAttributeValue("", "crowdname"));
		ex.setXmlns(parser.getNamespace());
		return ex;
	}

	public static class Extension implements PacketExtension {
		private StringBuilder sb = new StringBuilder();
		private String xmlns = "";

		private String crowdName;

		@Override
		public String getElementName() {

			return ELEMENT_NAME;
		}

		@Override
		public String getNamespace() {

			return xmlns;
		}

		public String getCrowdName() {
			return crowdName;
		}

		public void setCrowdName(String crowdName) {
			this.crowdName = crowdName;
		}

		public String getXmlns() {
			return xmlns;
		}

		public void setXmlns(String xmlns) {
			this.xmlns = xmlns;
		}

		@Override
		public String toXML() {
			sb.append("<").append(ELEMENT_NAME).append(" xmlns=\"").append(
					xmlns).append("\"").append(" crowdname=\"").append(
					crowdName).append("\"/>");

			return sb.toString();
		}

	}

}
